import showError from '../show'

import ajax from './ajax'


const app = getApp()
let code = null

export default {
  /**
   * 向后端请求
   * @returns Promises
   */
  login() {

    return app.wxp.login().then(res => {
      console.log(res)

      return ajax.post('user/login/', {
        code: res.code
      })
    })
    // 登录需要获取code，再post至后端，由后端取得openid和session_key
  },
  getWPPosts() {
    return ajax.get('wp-json/wp/v2/posts/', {
      categories: '5',
      _fields: 'id,date,modified,title,excerpt'
    })
  },
  getDevice() {
    return ajax.get('devices/', {
      // categories: '5',
      // _fields: 'id,date,modified,title,excerpt'
    })
  },
  registerDevice(ID, name, model) {
    return ajax.post('devices/register/', {
      name: name,
      ID: ID,
      model: model
    })
  },
  getWpPostDetail(id) {
    return ajax.get('/wp-json/wp/v2/posts/' + id, {
      categories: '5',
      _fields: 'id,content'
    })
  },
  getElectricData(campusName, buildingID, roomNo) {
    return ajax.post('power/inquiry/', {
      campusName: campusName,
      buildingID: buildingID,
      roomNo: roomNo,
    })
  },
  getDocs(option, itemNum, pk = '') {
    if (option) {
      if (category.indexOf(option) == -1) {
        return ajax.get('documents/' + pk, {
          department: option,
          category: null,
          limit: 30,
          offset: itemNum
        })
      } else {
        return ajax.get('documents/' + pk, {
          department: null,
          category: option,
          limit: 30,
          offset: itemNum
        })
      }
    } else {
      return ajax.get('documents/' + pk, {
        department: null,
        category: null,
        limit: 30,
        offset: itemNum
      })
    }
  },
  getDocContent(department, category, id) {
    return ajax.get('documents/' + id + '/', {
      department: department,
      category: category
    })
  },
  getDormitoryInfo(areaName, buildingID, roomNo) {
    return ajax.post('power/room/', {
      areaName: areaName,
      buildingID: buildingID,
      roomNo: roomNo
    })
  },

  saveSettings(config) {
    console.log(config, "ajax");

    return ajax.put('user/config/', config)
  },
  getSettings() {
    return ajax.get('user/config/')
  },
  ping() {
    return ajax.get('ping/')
  }
}